/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module;

import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.ivyde.common.ivyfile.IvyFileUpdater;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.Repository;
import org.openide.util.Exceptions;
import org.openide.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * Helper for editing descriptor file 
 * 
 * @author lforet
 */
public class IvyDescriptorHelper {

    private static IvyDescriptorHelper instance;
    private IvyFileUpdater ivyFileUpdater = new IvyFileUpdater();

    private IvyDescriptorHelper() {
    }

    public static IvyDescriptorHelper getInstance() {
        if (instance == null) {
            instance = new IvyDescriptorHelper();
        }
        return instance;
    }

    public void addDependency(FileObject descriptorFile, String org, String name, String revision, String conf) {
        try {
            ivyFileUpdater.addDependency(FileUtil.toFile(descriptorFile), org, name, revision, conf);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    
    public void removeDependency(FileObject descriptorFile, String org, String name) {
        try {
            ivyFileUpdater.removeOrExcludeDependency(FileUtil.toFile(descriptorFile), org, name);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    public FileObject createDescriptorTemplate(Project project) {
        try {
            FileObject projDir = project.getProjectDirectory();
            FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
            FileObject templateFO = sfs.findResource("Templates/ivybeans/ivy.xml"); // NOI18N
            assert templateFO != null;
            String ivyDescriptorName = FileUtil.findFreeFileName(projDir, "ivy", "xml");
            FileObject ivyDescriptorFile = FileUtil.copyFile(templateFO, projDir, ivyDescriptorName); // NOI18N
            Document ivyDescriptorDoc = XMLUtil.parse(new InputSource(ivyDescriptorFile.getInputStream()), false, true, null, null);

            NodeList retrieveElements = ivyDescriptorDoc.getElementsByTagName("info"); //NOI18N
            assert retrieveElements.getLength() == 1;
            Element retrieveElement = (Element) retrieveElements.item(0);
            String projectName = ProjectUtils.getInformation(project).getName();
            retrieveElement.setAttribute("organisation", projectName); // NOI18N
            retrieveElement.setAttribute("module", projectName); // NOI18N
            XMLUtil.write(ivyDescriptorDoc, new FileOutputStream(FileUtil.toFile(ivyDescriptorFile)), "UTF-8"); // NOI18N
            return ivyDescriptorFile;
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        } catch (SAXException ex) {
            throw new RuntimeException(ex);
        }
    }

    public FileObject createSettingsTemplate(Project project) {
        try {
            FileObject projDir = project.getProjectDirectory();
            FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
            FileObject templateFO = sfs.findResource("Templates/ivybeans/ivysettings.xml"); // NOI18N
            assert templateFO != null;

            FileObject ivyDescriptorFile = FileUtil.copyFile(templateFO, projDir, "ivysettings"); // NOI18N
            return ivyDescriptorFile;
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
